package com.qdlc.p2p.manage.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.WeChatMenuService;
import com.qdlc.p2p.biz.service.WeiXinService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dto.WeChatMenu;
import com.qdlc.p2p.dal.model.WeChatMenuModel;

/**
 * Created on 2016/2/3.
 */
public class ManageWeChatMenuAction extends BaseAction<WeChatMenuModel> implements ModelDriven<WeChatMenuModel> {

    @Resource
    private WeiXinService weiXinService;
    @Resource
    private WeChatMenuService weChatMenuService;

    private Map<String, Object> data;

    /**
     * 进入微信菜单列表页面
     *
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/column/weChatMenu/weChatManager")
    public String weChatManager() throws Exception {
        return "weChatManager";
    }

    /**
     * 微信菜单列表数据
     *
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/column/weChatMenu/weChatList")
    public void weChatList() throws Exception {
        data = new HashMap<String, Object>();
        List<WeChatMenu> list = weChatMenuService.listByIsDelete(false);
        data.put("rows", list);
        printJson(getStringOfJpaObj(data));
    }

    /**
     * 添加微信菜单页面
     *
     * @return 返回页面
     * @throws Exception 异常
     */
    @Action(value = "/modules/column/weChatMenu/weChatAddPage")
    public String weChatAddPage() throws Exception {
        long pid = paramLong("id"); // id设置成添加菜单的父id
        int level = (pid == -1) ? 1 : 2;
        pid = (pid == -1) ? 0 : pid;
        //直接传不过去？什么情况
        WeChatMenu weChatMenu = new WeChatMenu();
        weChatMenu.setLevel((byte)level);
        weChatMenu.setParentId(pid);
        request.setAttribute("weChatMenu", weChatMenu);
        return "weChatAddPage";
    }

    /**
     * 添加微信菜单
     *
     * @throws Exception 异常
     */
    @Action(value = "/modules/column/weChatMenu/doWeChatAdd")
    public void doWeChatAdd() throws Exception {
        boolean canBeAdd = checkParams(model);
        if (!canBeAdd) {
            printWebResult("字符长度不允许！", false);
            return;
        }
        String json = weiXinService.createWeChatMenu(model.getName(), model.getContent(), model.getParentId(), model.getLevel(), model.getButtonType());
        Gson gson = new Gson();
        Map map = gson.fromJson(json, Map.class);
        if (!(Boolean) map.get("result")) {
            printWebResult("修改菜单失败！", false);
            return;
        }
        printWebResult("修改菜单成功！", true);
    }

    /**
     * 修改微信菜单页面
     *
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/column/weChatMenu/weChatEditPage")
    public String weChatEditPage() throws Exception {
        long id = paramLong("id");
        WeChatMenu weChatMenu = weChatMenuService.findWeChatMenuById(id);
        request.setAttribute("weChatMenu", weChatMenu);
        return "weChatEditPage";
    }

    /**
     * 修改微信菜单
     *
     * @return
     * @throws Exception
     */
    @Action(value = "/modules/column/weChatMenu/doWeChatEdit")
    public void doWeChatEdit() throws Exception {
        if (StringUtil.isBlank(model.getId()) || StringUtil.isBlank(model.getButtonType()) || StringUtil.isBlank(model.getName()) || StringUtil.isBlank(model.getContent())) {
            printWebResult("参数为空", false);
            return;
        }
        boolean canBeAdd = checkParams(model);
        if (!canBeAdd) {
            printWebResult("字符长度不允许！", false);
            return;
        }
        String json = weiXinService.updateWeChatMenu(model.getId(), model.getName(), model.getContent(), model.getButtonType());
        Gson gson = new Gson();
        Map map = gson.fromJson(json, Map.class);
        if (!(Boolean) map.get("result")) {
            printWebResult("修改菜单失败！", false);
            return;
        }
        printWebResult("修改菜单成功！", true);
    }

    /**
     * 删除菜单
     *
     * @return
     */
    @Action("/modules/column/weChatMenu/doWeChatDel")
    public void doWeChatDel() throws IOException {
        long menuId = model.getId();
        if (menuId == 0) {
            printWebResult("删除菜单不存在", false);
            return;
        }
        String json = weiXinService.deleteWeChatMenu(menuId);
        Gson gson = new Gson();
        Map map = gson.fromJson(json, Map.class);
        if (!(Boolean) map.get("result")) {
            printWebResult("删除菜单失败！", false);
            return;
        }
        printWebResult("删除菜单成功！", true);
    }


    public boolean checkParams(WeChatMenuModel model) {
        //一级菜单长度小于4，二级菜单长度小于8
        if (model.getLevel() == 1 && model.getName().toCharArray().length > 4) {
            return false;
        } else if (model.getLevel() == 2 && model.getName().toCharArray().length > 8) {
            return false;
        }
        ;
        //校验网址,buttonType=1为url链接
//        if (model.getButtonType() == 1) {
//            String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
//            Pattern pattern = Pattern.compile(regex);
//            Matcher matcher = pattern.matcher(regex);
//            if (!matcher.matches())
//                return false;
//        }
        //内容不能为空
        return StringUtil.isNotBlank(model.getContent());
    }

}
